import globalConst from "../../common/globalConst";
var global = globalConst.GlobalConst;

import pathlink from "../../graph/pathlink";
import coordPkg from "../../common/struct/coord";
import fillCommStru from "../../common/fillCommStru";


//符号“$”隔开多个环，第一个环为外环；
//符号“@”隔开环内的子图元
//各子图元根据不同的类型有不同的描述方式
//"line,x1,y1;x2,y2;....@arc,cntx,cnty,radius,stangle,endangle,anticlockwise@bezier,st,ctrlPt1,
//ctrlPt2,end@quadratic,st,end,ctrlPt$line,x1,y1;x2,y2;....@arc,cntx,cnty,radius,stangle,endangle,anticlockwise" 

var PathGraphCopySvc = {}

PathGraphCopySvc.copyPathProp = function (info, graph, offsetx, offsety) {
    // console.log("PathGraphCopySvc.copyPathProp********************************");
    let pathLinkDescList = info.pathDesc.split(global.LINKMARK);
    if (pathLinkDescList.length == 0) {
        return -1;
    }
    let pathGraphPos = graph.getPos();
    pathGraphPos.pathLinkList = [];
    graph.m_PathLinkList.length = 0;
    for (let i = 0; i < pathLinkDescList.length; i++) {
        let innerLinkDesc = pathLinkDescList[i];
        this.parseLinkFrmCoordDesc(innerLinkDesc, graph, offsetx, offsety);
    }
    let fillDef = graph.getFillDef();
    fillDef.translate(offsetx, offsety, true);
    return 0;
}


PathGraphCopySvc.parseLinkFrmCoordDesc = function (coordDesc, graph, offsetx, offsety) {
    let unitCoordDescList = coordDesc.split(global.LINK_UNIT_MARK);
    let count = unitCoordDescList.length;
    for (let i = count - 1; i >= 0; i--) {
        let t = unitCoordDescList[i];
        t = t.trim();
        if (t == "") {
            unitCoordDescList.splice(i, 1);
        }
    }
    count = unitCoordDescList.length;
    let firstUnitDesc = "";
    if (count == 0) {
        firstUnitDesc = coordDesc;
    }
    else {
        firstUnitDesc = unitCoordDescList[0];
    }
    if (firstUnitDesc == "") {
        return null;
    }

    let pathUnitList = [];
    for (let i = 0; i < count; i++) {
        let subCoordDesc = unitCoordDescList[i];
        subCoordDesc = subCoordDesc.trim();
        if (subCoordDesc != "") {
            let unit = this.parsePathUnit(subCoordDesc);
            if (unit && unit.coordDecs) {
                unit.coordDecs.offset(offsetx, offsety);
                pathUnitList.push(unit);
            }
        }
    }
    if (pathUnitList.length == 0) {
        return null;
    }
    // console.log(pathUnitList);
    let pathLinkList = this.createPathEdgeLink(pathUnitList);
    let outLink = pathLinkList[0];
    if (outLink) {
        graph.addPathLink(outLink);
        let pathGraphPos = graph.getPos();
        pathGraphPos.x = 0;
        pathGraphPos.y = 0;
    }
}

PathGraphCopySvc.parsePathUnit = function (coordDesc) {
    let idx = coordDesc.indexOf(",");
    if (idx == -1) {
        return null;
    }
    let type = coordDesc.substr(0, idx);
    let coord = coordDesc.substr(idx + 1, coordDesc.length - idx - 1);
    let pathunit = new pathlink.PathUnit();
    pathunit.type = type;
    pathunit.coordDecs = this.parsePosObj(type, coord);
    return pathunit;
}

PathGraphCopySvc.parsePosObj = function (type, coord) {
    let pos = null;
    if (type == fillCommStru.PathUnitType.line) {
        pos = new coordPkg.MultiPos();
    }
    else if (type == fillCommStru.PathUnitType.arc) {
        pos = new coordPkg.ArcPos();
    }
    else if (type == fillCommStru.PathUnitType.spline
        || type == fillCommStru.PathUnitType.bezier
        || type == fillCommStru.PathUnitType.quadratic
    ) {
        pos = new coordPkg.MultiPos();
    }
    else if (type == fillCommStru.PathUnitType.ellipseArc) {
        pos = new coordPkg.EllipseArcPos();
    }
    if (pos) {
        pos.fromString(coord);
    }
    else {
        console.log("unkown coord type");
    }

    return pos;
}

PathGraphCopySvc.createPathEdgeLink = function (pathUnitList) {
    let size = pathUnitList.length;
    let startPathunit = pathUnitList[0];
    let preUnit = startPathunit;
    for (let i = 1; i < size; i++) {
        let nextUnit = pathUnitList[i];
        preUnit.nextUnit = nextUnit;
        preUnit = nextUnit;
    }

    let pathLink = new pathlink.PathUnitLink();
    pathLink.setPathStartUnit(startPathunit);

    let linkList = [];
    linkList.push(pathLink);
    return linkList;
}


export default { PathGraphCopySvc }